package org.benjamin.lending.controller;

import org.benjamin.lending.domain.BJUIData;
import org.benjamin.lending.domain.BasicIndex;
import org.benjamin.lending.domain.MemberLogIndex;
import org.benjamin.lending.domain.entity.BorrowTender;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.MemberMoneyLog;
import org.benjamin.lending.form.TenderLogIndex;
import org.benjamin.lending.service.BorrowService;
import org.benjamin.lending.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * Created by xywenjie on 2017/5/30.
 */
@Controller
@RequestMapping("member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private BorrowService borrowService;

    @GetMapping("index")
    public String index(){
        return "member/index";
    }

    @ResponseBody
    @PostMapping("data")
    public BJUIData data(BasicIndex basicIndex){
        Page<MemberInfo> memberInfos = this.memberService.findMemberAll(basicIndex.getPageRequest());
        BJUIData bjuiData = new BJUIData(memberInfos);
        return bjuiData;
    }
    
    @RequestMapping(value = "details")
    public String details(Integer id,ModelMap modelMap){
    	MemberInfo memberInfo = this.memberService.getEntityId(id,true);
    	modelMap.addAttribute("memberInfo",memberInfo);
    	return "member/details";
    }
    
    @RequestMapping(value = "moneyLogs")
    public String toMoneyLog(MemberLogIndex moneyLogIndex,ModelMap modelMap){
        modelMap.addAttribute("moneyLogIndex",moneyLogIndex);
        Page<MemberMoneyLog> memberMoneyLogs = this.memberService.findByMoneyLogsByUserId(moneyLogIndex.getId(),moneyLogIndex.getPageRequest());
        modelMap.addAttribute("memberMoneyLogs",memberMoneyLogs);
    	return "member/moneyLog";
    }

    @ResponseBody
    @RequestMapping(value = "moneyLogsData")
    public BJUIData moneyLogsData(MemberLogIndex idLogIndex){
        Page<MemberMoneyLog> memberMoneyLogs = this.memberService.findByMoneyLogsByUserId(idLogIndex.getId(),idLogIndex.getPageRequest());
        BJUIData bjuiData = new BJUIData(memberMoneyLogs);
        return bjuiData;
    }

    /**
     * 用户的投资记录
     * @return
     */
    @RequestMapping(value = "tenderLog")
    public String tenderLog(@ModelAttribute("tenderLogIndex") TenderLogIndex tenderLogIndex, ModelMap modelMap){
        Page<BorrowTender> borrowTenders = this.borrowService.findUserTenderLogPage(tenderLogIndex);
        modelMap.addAttribute("borrowTenders",borrowTenders);
        return "member/tenderLog";
    }
}
